home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Cafe 3
/
Visual Cafe 3.ISO
/
Vcafe
/
Main.bin
/
FileTableModel.java
< prev
next >
Wrap
Text File
|
1998-10-07
|
7KB
|
228 lines
package com.symantec.itools.swing.models;
import java.io.*;
import java.net.URL;
import java.math.*;
import java.util.Vector;
import java.util.StringTokenizer;
// A table model that uses a tab, comma or space - delimited file as input.
// Excess column headers beyond rowCount, which is pinned to the widest data
// row, are ignored by JTable and not shown.
public class FileTableModel
extends com.sun.java.swing.table.AbstractTableModel
implements Serializable
{
public static final int TAB_DELIMITED = 1;
public static final int COMMA_DELIMITED = 2;
public static final int SPACE_DELIMITED = 3;
protected static final int POSSIBLE_DELIMITERS = 3;
protected int rowCount = 0;
protected int colCount = 0;
protected int numHeaders = 0;
protected Vector dataVector = new Vector(rowCount);
protected Vector headerVector = new Vector(colCount);
protected URL dataURL;
protected int delimiter = TAB_DELIMITED;
protected String delimiterString = "\t";
//
// com.sun.java.swing.table.TableModel implementation
//
// Returns number of rows of data in table.
public int getRowCount()
{
return rowCount;
}
// Returns the number of columns of data (not headers)
public int getColumnCount()
{
return colCount;
}
//Returns columns header; a single space if it has been padded.
public String getColumnName(int column)
{
return (String)headerVector.elementAt(column);
}
// JTable can hold any object, but in this model will always
// return a class of String.
public Class getColumnClass(int columnIndex)
{
return String.class;
}
// Return value from a specified cell.
public Object getValueAt(int row, int column)
{
return (((Vector)dataVector.elementAt(row)).elementAt(column));
}
// Set value in a specified cell.
// Does not filter out non-String objects.
public void setValueAt(Object aValue, int row, int column)
{
((Vector)dataVector.elementAt(row)).setElementAt(aValue, column);
fireTableCellUpdated(row,column);
}
// Return false
public boolean isCellEditable(int rowIndex, int columnIndex)
{
return false;
}
//
// Properties
//
// Return the URL for this models associated file.
public URL getItems()
{
return dataURL;
}
// Takes a URL to a tab, comma or space-delimited text file.
// Pads all rows to length of longest row.
// Pads headers with spaces to length of longest row.
public void setItems(URL newItems)
{
dataVector = new Vector();
dataURL = newItems;
if(dataURL != null)
{
try
{
colCount = 0;
BufferedReader input = new BufferedReader(new InputStreamReader((InputStream)newItems.getContent()));
StringTokenizer currentLine;
Vector newRow = new Vector();
String nextLine;
while((nextLine = input.readLine())!= null)
{
currentLine = new StringTokenizer(nextLine,delimiterString);
newRow = new Vector();
while(currentLine.hasMoreTokens())
{
newRow.addElement(currentLine.nextToken());
if (newRow.size() > numHeaders)
{
//pad headers if needed
headerVector.addElement(" ");
numHeaders++;
}
}
colCount = Math.max(newRow.size(),colCount);
dataVector.addElement(newRow);
}
rowCount = dataVector.size();
// pad any short rows
for(int i = 0; i < dataVector.size();i++)
{
int difference = colCount - ((Vector)dataVector.elementAt(i)).size();
if(difference >0)
for(int j = 0; j< difference; j++)
{
((Vector)dataVector.elementAt(i)).addElement("");
}
}
input.close();
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
fireTableStructureChanged();
fireTableDataChanged();
}
else
{
Vector emptyRow = new Vector(colCount);
for(int i = 0; i < Math.max(colCount,numHeaders);i++)
emptyRow.addElement("");
dataVector.addElement(emptyRow);
fireTableStructureChanged();
fireTableDataChanged();
}
}
// Return column headers as a comma-delimited String.
public String getColumnHeaders()
{
StringBuffer temp = new StringBuffer();
for(int i = 0; i < headerVector.size();i++)
{
if (temp.length() > 0)
{
temp.append(",");
}
temp.append(headerVector.elementAt(i));
}
return temp.toString();
}
// Takes a comma-delimited string and converts it too column headers.
// If headers are shorter than data (colCount) they are padded with a space.
public void setColumnHeaders(String newHeaders)
{
if (newHeaders == null)
newHeaders = "";
StringTokenizer headers = new StringTokenizer(newHeaders,",");
headerVector = new Vector();
numHeaders = headers.countTokens();
for (int j = 0; j < Math.max(colCount,numHeaders);j++)
{
if (headers.hasMoreTokens())
headerVector.addElement( headers.nextToken());
else // Rod, leave the space or the headers won't draw.
headerVector.addElement(" ");
}
fireTableStructureChanged();
fireTableDataChanged();
}
// Return the deliimiter.
// One of TAB_DELIMITED, COMMA_DELIMITED or SPACE_DELIMITED.
public int getDelimiter()
{
return delimiter;
}
// Set the deliimiter.
// One of TAB_DELIMITED, COMMA_DELIMITED or SPACE_DELIMITED.
public void setDelimiter(int d)
throws IllegalArgumentException
{
if(0 < d && d < (POSSIBLE_DELIMITERS+1))
delimiter = d;
else
throw new IllegalArgumentException("delimiter must be between 1 and "+POSSIBLE_DELIMITERS+" inclusive");
switch(delimiter)
{
case TAB_DELIMITED: {delimiterString = "\t";break;}
case COMMA_DELIMITED: {delimiterString = ",";break;}
case SPACE_DELIMITED: {delimiterString = " ";break;}
default : {delimiterString = "\t";break;}
}
setColumnHeaders(getColumnHeaders());
setItems(getItems());
fireTableStructureChanged();
fireTableDataChanged();
}
//{{DECLARE_CONTROLS
//}}
}